package com.whz.workmanageol.utils;

import com.whz.workmanageol.types.ExcelType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 管理员下载Excel信息模板 便于信息导入
 */
@Component
public class ExcelTempCreate {
    /**
     * 创建信息Excel模板
     */
    public void createInfo(HttpServletResponse response, ExcelType excelType){
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        // 第一行文字说明
        Row row = sheet.createRow(0);
        String filename = null;
        switch (excelType){

            case STUDENT : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("学号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("密码");

                cell = row.createCell(2, CellType.STRING);
                cell.setCellValue("姓名");

                cell = row.createCell(3, CellType.STRING);
                cell.setCellValue("电子邮箱");

                cell = row.createCell(4, CellType.STRING);
                cell.setCellValue("电话号码");

                filename = "studentinfo.xlsx";
                break;
            }

            case TEACHER : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("工号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("密码");

                cell = row.createCell(2, CellType.STRING);
                cell.setCellValue("姓名");

                cell = row.createCell(3, CellType.STRING);
                cell.setCellValue("电子邮箱");

                cell = row.createCell(4, CellType.STRING);
                cell.setCellValue("电话号码");

                filename = "teacherinfo.xlsx";
                break;
            }

            case GRADE : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("班级编号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("班级名称");

                filename = "gradeinfo.xlsx";
                break;
            }

            case COURSE : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("课程编号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("课程名称");

                filename = "courseinfo.xlsx";
                break;
            }

            case GRADECOURSE : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("班级名称");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("课程名称");

                filename = "gradecourseinfo.xlsx";
                break;
            }

            case TEAGRADE : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("教师工号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("班级名称");

                filename = "teachergradeinfo.xlsx";
                break;
            }

            case TEACOURSE : {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("教师工号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("课程名称");

                filename = "teachercourseinfo.xlsx";
                break;
            }

            case STUGRADE: {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("学生学号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("班级名称");

                filename = "studentgradeinfo.xlsx";
                break;
            }

            case STUCOURSE: {
                Cell cell = row.createCell(0, CellType.STRING);
                cell.setCellValue("学生学号");

                cell = row.createCell(1, CellType.STRING);
                cell.setCellValue("课程名称");

                filename = "studentcourseinfo.xlsx";
                break;
            }
        }
//        response.setHeader("Content-disposition", "attachment;filename=" + filename);
//        response.setContentType("application/octet-stream");
//        response.setContentType("application/vnd.ms-excel");
//        response.setContentType("application/msexcel");
        DownloadSet.setContent(response, filename);
        try(OutputStream os = response.getOutputStream()) {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
